package com.site.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import com.site.dao.backend.system.MenuDao;
import com.site.dto.backend.system.ResourseBizBean;
import com.site.dto.backend.system.RoleBizBean;
import com.site.service.backend.system.MenuService;
import com.site.service.backend.system.RoleService;
import com.site.util.constant.Constants;

/**
 * 菜单缓存
 * @author 
 */
@SuppressWarnings("all")
public class MenuUtil {
	
	private MenuService menuService;
	private MenuDao menuDao;
	private RoleService roleService;
	
	private ApplicationContext springCtx = null;
	
	public void loadMenu(ServletContext context)
	{
		springCtx=WebApplicationContextUtils.getWebApplicationContext(context);
		// 所有菜单
		if(menuService==null){
			menuService=(MenuService)springCtx.getBean("menuService");
		}
		List<ResourseBizBean> menuList =menuService.getMenu(null);		
		context.setAttribute(Constants.COMMON_MENU, menuList);
		
		//角色菜单
		if(roleService==null){
			roleService=(RoleService)springCtx.getBean("roleService");
		}
		List<RoleBizBean> roleList =roleService.getRoleList(null,null);
		if (roleList != null && roleList.size() > 0) {
			Map<Long,Set<ResourseBizBean>> roleMenuList = new HashMap<Long,Set<ResourseBizBean>>();
			for (int i = 0; i < roleList.size(); i++) {
				RoleBizBean role = roleList.get(i);
				if(null != role){
					List<ResourseBizBean> resourseBizBeans = roleService.getRoleResourse(role.getId());
					if(null != resourseBizBeans && resourseBizBeans.size() > 0){
						Set<ResourseBizBean> resourseBizBeanList = new HashSet<ResourseBizBean>(); 
						resourseBizBeanList.addAll(resourseBizBeans);
						roleMenuList.put(role.getId(), resourseBizBeanList);
					}
				}
			}
			context.setAttribute(Constants.ROLE_MENU, roleMenuList);
		}
	}
	/**
	 * 获取所有菜单
	 * @param request
	 * @return
	 */
	public List<ResourseBizBean> getAllMenu(HttpServletRequest request) {
		return (List<ResourseBizBean>)request.getSession().getServletContext().getAttribute(Constants.COMMON_MENU);
	}
	  
	/**
	 * 添加菜单
	 */
	public void addMenu(ResourseBizBean menu, HttpServletRequest request) {
		if (menu == null || menu.getId() == null) {
			return;
		}
		List<ResourseBizBean> menuList = getAllMenu(request);
		if (menuList == null) {
			menuList = new ArrayList<ResourseBizBean>();
		}
		if (!menuList.contains(menu)) {
			menuList.add(menu);
			request.getSession().getServletContext().setAttribute(Constants.COMMON_MENU, menuList);
		}
		
	}
	
  
	/**
	 * 更新菜单
	 */
	public void updateMenu(ResourseBizBean menu, HttpServletRequest request) {
		if (menu == null || menu.getId() == null) {
			return;
		}
		loadMenu(request.getSession().getServletContext());
	}
	
	
	private static MenuUtil instance;
	public static MenuUtil getInstance()
	{
		if(instance==null)
			instance=new MenuUtil();
		return instance;
	}
	/**
	 * 获得角色菜单
	 * @param id
	 * @param request
	 * @return
	 */
	public Set<ResourseBizBean> getRoleMenu(Long id, HttpServletRequest request) {
		if (id == null) {
			return null;
		}
		Map<Long,Set<ResourseBizBean>> roleMenu = (Map<Long,Set<ResourseBizBean>>)request.getSession().getServletContext().getAttribute(Constants.ROLE_MENU);
		if(roleMenu != null){
			return roleMenu.get(id);
		}
		return null;
	}
}
